"""
Case Type   : DML
Case Name   : 配置参数enable_default_ustore_table=on/ ustore_attr='enable_ustore_partial_seqscan=false',运行一条update操作
Create At   : 2024/10/30
Owner       : nanyang12
Description :
        1.配置参数enable_default_ustore_table=on/ ustore_attr='enable_ustore_partial_seqscan=false'
        2.建表并插入数据
        3.执行update操作
        4.清理环境，恢复参数默认值
Expect      :
        1.成功
        2.执行成功
        3.执行成功
        4.默认值恢复成功
History     :
"""
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

LOG = Logger()
commonsh = CommonSH('dbuser')


class DmlUpdate(unittest.TestCase):
    def setUp(self):
        self.constant = Constant()
        self.user_node = Node('dbuser')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        LOG.info(
            '------Opengauss_Function_DML_Update_Case0034start----')

    def test_update(self):
        LOG.info(
            '--步骤1:配置参数enable_default_ustore_table=on/ ustore_attr=\'enable_ustore_partial_seqscan=false\'--')
        msg = commonsh.execute_gsguc('set',
                                     self.constant.GSGUC_SUCCESS_MSG,
                                     'enable_default_ustore_table=on')
        LOG.info(msg)
        self.assertTrue(msg)
        msg = commonsh.execute_gsguc('set',
                                     self.constant.GSGUC_SUCCESS_MSG,
                                     'ustore_attr=\'\'')
        LOG.info(msg)
        self.assertTrue(msg)
        msg = commonsh.restart_db_cluster()
        LOG.info(msg)
        status = commonsh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)
        LOG.info('--步骤2~3:建表并插入数据,执行update操作--')
        sql_cmd = '''drop table if exists abort_test;
                     create table abort_test(id int,name varchar(10));
                     insert into abort_test values(1,'a');
                     update abort_test set abort_test.name='cici' where abort_test.id=1;
                    '''
        excute_cmd1 = f'''source {self.DB_ENV_PATH};
            gsql -d postgres -p {self.user_node.db_port} -c "{sql_cmd}"
            '''
        LOG.info(excute_cmd1)
        msg1 = self.user_node.sh(excute_cmd1).result()
        LOG.info(msg1)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, msg1)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg1)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, msg1)
        self.assertIn(self.constant.UPDATE_SUCCESS_MSG, msg1)

    def tearDown(self):
        LOG.info('--步骤4:恢复默认值--')
        msg = commonsh.execute_gsguc('set',
                                     self.constant.GSGUC_SUCCESS_MSG,
                                     'enable_default_ustore_table=off')
        LOG.info(msg)
        self.assertTrue(msg)
        msg = commonsh.execute_gsguc('set',
                                     self.constant.GSGUC_SUCCESS_MSG,
                                     'ustore_attr=\'ustore_verify_level=FAST;ustore_verify_module=UPAGE:UBTREE\'')
        LOG.info(msg)
        self.assertTrue(msg)
        msg = commonsh.restart_db_cluster()
        LOG.info(msg)
        status = commonsh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)
        LOG.info(
            '------Opengauss_Function_DML_Update_Case0034finish----')
